package com.whu.zqm.controller;

import com.whu.zqm.entity.Products;
import com.whu.zqm.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("products")
public class ProductController {

    @Autowired
    ProductsService productsService;

    @GetMapping("/id/{id}")
    public ResponseEntity<Products> getProducts(@PathVariable long id) {
        Products res = productsService.getProduct(id);
        if (res == null) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.ok(res);
        }
    }

    @PostMapping("/add")
    public ResponseEntity<String> addProducts(@RequestBody Products p) {
        try {
            Products res = productsService.addProduct(p);
            return ResponseEntity.ok("添加成功！" + res.getId());
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @PutMapping("/update/{id}")
    public ResponseEntity<Void> updateTodo(@PathVariable long id,@RequestBody Products p){
        productsService.updateProducts(id,p);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> deleteTodo(@PathVariable long id){
        productsService.deleteProducts(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/name/{name}")
    public ResponseEntity<List<Products>> getProductByName(@PathVariable String name){
        List<Products> res = productsService.findByName(name);
        if (res == null) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.ok(res);
        }
    }

    //以上是基础增删改查，后面实现模糊查询等其他复杂查询方法

    //jpql查找所有有库存的
    @GetMapping("/instock")
    public ResponseEntity<List<Products>> getProductsInStock(){
        List<Products> res = productsService.getProductInStock();
        if (res == null) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.ok(res);
        }
    }

    @GetMapping("/all")
    public ResponseEntity<Page<Products>> getAll(){
        Pageable p = PageRequest.of(0, 3);
        Page<Products> res = productsService.getAll(p);
        if (res == null) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.ok(res);
        }
    }

    @PatchMapping("/{id}/addSupplier")
    public ResponseEntity<Products> addSupplier(@PathVariable long id, @RequestParam long supplierId) {
        Products p = productsService.addSupplier(id, supplierId);
        if (p != null) {
            return ResponseEntity.ok(p);
        }
        else {
            return ResponseEntity.notFound().build();
        }
    }

    @PatchMapping("/{id}/removeSupplier")
    public ResponseEntity<Products> removeSupplier(@PathVariable long id, @RequestParam long supplierId) {
        Products p = productsService.removeSupplier(id, supplierId);
        if (p != null) {
            return ResponseEntity.ok(p);
        }
        else {
            return ResponseEntity.notFound().build();
        }
    }

}

